<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  barData: {
    type: Object,
    default: () => {}
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    let option = {
      tooltip: {
        trigger: 'axis',
        formatter: (val) => {
          return (
            `${val[0].name}` + '<br>' +
            `投资金额： ${val[0].value}${props?.barData?.yAxisLeftLabel}`
          )
        }
      },
      barMaxWidth: pxToRem(11 * props?.dialogData?.fontSizeTimes),
      grid: {
        left: `${props?.dialogData?.fontSizeTimes === 1 ? 3 : 5}%`,
        right: `${props?.dialogData?.fontSizeTimes === 1 ? 3 : 5}%`,
        bottom: `${props?.dialogData?.fontSizeTimes === 1 ? 3 : 0}%`,
        top: props?.dialogData?.fontSizeTimes === 1 ? '20%' : '10%',
        containLabel: true
      },
      dataZoom: { // 数据过多内部拖动逻辑
        type: "inside",
        zoomLock: true, // 不可缩放
        startValue: 0,
        endValue: props?.dialogData?.fontSizeTimes === 1 ? 7 : 5,
      },
      xAxis: {
        type: 'category',
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false, // 不显示坐标轴线
        },
        splitLine: {
          show: false // 不显示网格线
        },
        axisLabel: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          interval: 0
        },
        data: props?.barData?.xAxisData
      },
      yAxis: {
        name: `单位：${props?.barData?.yAxisLeftLabel}`,
        nameGap: pxToRem(props?.dialogData?.fontSizeTimes === 1 ? 15 : 25),
        min: 0,
        nameTextStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          padding: [0, 0, 0, pxToRem(props?.dialogData?.fontSizeTimes === 1 ? 50 : 15)]
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false, // 不显示坐标轴线
        },
        splitLine: {
          show: false // 不显示网格线
        },
        axisLabel: {
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          color: 'white'
        },
        position: 'left',
      },
      series: {
        type: 'bar',
        data: props?.barData?.seriesData
      }
    }
    option && myChart.value.setOption(option, true);
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};

defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h291 {
  height: 291px;
}
.dialog-h291 {
  width: 1120px;
  height: 500px;
}
</style>